package com.mdisoft.quotogram.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class UserSettings {

	private SharedPreferences _p;
	private SharedPreferences.Editor _e;
	
	public UserSettings(Context context) {
		_p = context.getSharedPreferences("quotogram", 0);
	}
	
	
	/**
	 * Get username
	 * @return String
	 */
	public String getUsername() {
		return _p.getString("username", "");
	}
	/**
	 * Set username
	 * @param String
	 */
	public void setUsername(String value) {
		if (_e==null) _e=_p.edit();
		_e.putString("username", value);
		_e.commit();
	}
	
	
	/**
	 * Get password
	 * @return String
	 */
	public String getPassword() {
		return _p.getString("password", "");
	}
	/**
	 * Set password
	 * @param String
	 */
	public void setPassword(String value) {
		if (_e==null) _e=_p.edit();
		_e.putString("password", value);
		_e.commit();
	}


	/**
	 * Get latitude
	 * @return float
	 */
	public float getLatitude() {
		return _p.getFloat("latitude", 0.0f);
	}
	/**
	 * Set latitude
	 * @param float
	 */
	public void setLatitude(float value) {
		if (_e==null) _e=_p.edit();
		_e.putFloat("latitude", value);
		_e.commit();
	}
	
	
	/**
	 * Get longitude
	 * @return float
	 */
	public float getLongitude() {
		return _p.getFloat("longitude", 0.0f);
	}
	/**
	 * Set longitude
	 * @param float
	 */
	public void setLongitude(float value) {
		if (_e==null) _e=_p.edit();
		_e.putFloat("longitude", value);
		_e.commit();
	}


	/**
	 * Check if database was updated less than minute ago
	 * @return true if database is fresh, otherwise - false
	 */
	public boolean isDbFresh() {
		if (System.currentTimeMillis() - _p.getLong("lastUpdate", 0) < 60*1000L) 
			return true;
		else
			return false;
	}
	/**
	 * Get time of last database update
	 * @return time in milliseconds (long)
	 */
	public long getTimeOfLastUpdate() {
		return _p.getLong("lastUpdate", 0);
	}
	/**
	 * Set time of last database update
	 */
	public void setTimeOfLastUpdate() {
		if (_e==null) _e=_p.edit();
		_e.putLong("lastUpdate", System.currentTimeMillis());
		_e.commit();
	}
	
	
	/**
	 * Get location privacy settings
	 * @return 0 - show; 1 - hide
	 */
	public int getSettingsLocationPrivacy() {
		return _p.getInt("LocationPrivacy", 1);
	}
	/**
	 * Set location privacy
	 * @param 0 - show; 1 - hide
	 */
	public void setSettingsLocationPrivacy(int value) {
		if (_e==null) _e=_p.edit();
		_e.putInt("LocationPrivacy", value);
		_e.commit();
	}

}
